package cn.hp.controller;

import cn.hp.common.constant.Constant;
import cn.hp.entity.DictType;
import cn.hp.service.DictTypeService;
import cn.hp.util.util.RandomNumber;
import cn.hp.util.util.RedisUtil;
import cn.hp.util.util.Result;
import com.alibaba.druid.sql.visitor.functions.If;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/dictType")
@Api(value = "/dictType",tags = "字典类型管理")
public class DictTypeController {

    @Autowired
    private DictTypeService dictTypeService;

    @Resource
    private RedisUtil redisUtil;


    @GetMapping
    @ApiOperation(value = "查询",notes = "查询所有字典类型")
    public Result find(DictType dictType){
        //先检查redis缓存，查看redis缓存是否存在字典类型,DICTTTPE_KEY
        //如果存在，直接从缓存中获取
        //如果不存在，连接数据库，从数据库中查询出来，并放入缓存中

        //如果redis中没有DICTTTPE_KEY
        if (!redisUtil.hasKey("DICTTTPE_KEY")){
            //将字典类型从数据库中查出
        List<DictType>dictTypeList=dictTypeService.selectDictType(dictType);
        //放入redis中进行缓存
        redisUtil.set("DICTTTPE_KEY", JSON.toJSONString(dictTypeList), RandomNumber.randomNum(null));
        }
        //如果redis中有DICTTTPE_KEY，从缓存中取出DICTTTPE_KEY
        Object obj=redisUtil.get("DICTTTPE_KEY");
        //解析obj字符串为对象
        List<DictType> list=JSON.parseArray((String) obj,DictType.class);

        return Result.success(list);

    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询",notes = "分页查询所有字典类型")
    public Result page(DictType dictType, @RequestParam(value = "page" ,defaultValue = "1") Integer page,@RequestParam(value = "limit",defaultValue = "10") Integer limit){
        PageHelper.startPage(page ,limit);
        PageInfo<DictType>dictTypePageInfo=dictTypeService.selectDictType(dictType,page,limit);
        return Result.success(dictTypePageInfo);
    }

    @PostMapping
    @ApiOperation(value = "新增",notes = "新增字典类型")
    public  Result insert(@RequestBody DictType dictType){
        redisUtil.del(Constant.DICTTTPE_KEY);
        int i= dictTypeService.insertDictType(dictType);
        return i>0 ? Result.success("添加成功") : Result.error("添加失败");
    }

    @PutMapping
    @ApiOperation(value = "修改",notes = "修改字典类型")
     public  Result update(@RequestBody DictType dictType){
         redisUtil.del(Constant.DICTTTPE_KEY);
        int i=dictTypeService.updateDictType(dictType);
        return i>0 ? Result.success("修改成功") : Result.error("修改失败");
    }


    @DeleteMapping("/{id}")
        @ApiOperation(value = "删除",notes = "删除字典类型")
     public  Result delete(@PathVariable Integer id){
         redisUtil.del(Constant.DICTTTPE_KEY);
       int i=dictTypeService.deleteDictTypeById(id);
        return i>0 ? Result.success("删除成功") : Result.error("删除失败");
    }


}
